package test;

import java.util.Stack;

/**
 * 描述：
 *      二叉树的非递归遍历
 * @author hl
 * @version 1.0
 * @date 2020/11/18 10:18
 */
public class TreeTraverse {

    public static void preOrder(Node head){
        Stack<Node> stack = new Stack<>();
        stack.push(head);
        while(!stack.isEmpty()){
            Node node = stack.pop();
            System.out.print(node.value + " ");
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
    }
    public static void inOrder(Node head){
        if (head != null) {
            Stack<Node> stack = new Stack<>();
            Node cur = head;
            while(!stack.isEmpty() || cur != null){
               if (cur != null) {
                   stack.push(cur);
                   cur = cur.left;
               }else{
                   cur = stack.pop();
                   System.out.print(cur.value + " ");
                   cur = cur.right;
               }
            }
        }
    }
    public static void posOrder(Node head){
        if (head != null) {
            Stack<Node> s1 = new Stack<>();
            Stack<Node> s2 = new Stack<>();
            Node cur = head;
            s1.push(cur);
            while(!s1.isEmpty()){
                cur = s1.pop();
                s2.push(cur);
                if (cur.left != null) {
                    s1.push(cur.left);
                }
                if (cur.right != null) {
                    s1.push(cur.right);
                }
            }
            while(!s2.isEmpty()){
                System.out.print(s2.pop().value + " ");
            }
        }
    }

    public static void main(String[] args) {
        Node head = new Node(5);
        head.left = new Node(3);
        head.right = new Node(8);
        head.left.left = new Node(2);
        head.left.right = new Node(4);
        head.left.left.left = new Node(1);
        head.right.left = new Node(7);
        head.right.left.left = new Node(6);
        head.right.right = new Node(10);
        head.right.right.left = new Node(9);
        head.right.right.right = new Node(11);

        // recursive
        System.out.println("==============recursive==============");
        System.out.print("pre-order: ");
        preOrder(head);
        System.out.println();
        System.out.print("in-order: ");
        inOrder(head);
        System.out.println();
        System.out.print("pos-order: ");
        posOrder(head);
//        preOrderRecur(head);
//        System.out.println();
//        System.out.print("in-order: ");
//        inOrderRecur(head);
//        System.out.println();
//        System.out.print("pos-order: ");
//        posOrderRecur(head);
//        System.out.println();
//
//        // unrecursive
//        System.out.println();
//        System.out.println("============unrecursive=============");
//        preOrderUnRecur(head);
//        System.out.println();
//        inOrderUnRecur(head);
//        System.out.println();
//        posOrderUnRecur(head);
    }

}
